#include"client.h"

void do_register(int sockfd,MSGA *msga)
{	
	extern su;
	msga->type=R;						//type
	puts("input name>>>");
	scanf("%s",msga->name);					//name
	puts("input pwd>>>");
	scanf("%s",msga->password);				//password
	puts("please input your information: input age>>>");
	scanf("%s",msga->age);							//age
	puts("input addr>>>");
	scanf("%s",msga->addr);	//addr
	/*
	if(0 == su)
	{
		strcpy(msga->idter,"ssuper");
	}
	else
	{	
		strcpy(msga->idter,"normal");
	}
	su++;
	*/
	send(sockfd,msga,LEN_SMG,0);//用户名密码发给服务器
	printf("msga-send ok\n");
	recv(sockfd,msga,LEN_SMG,0); //等待服务器回应
	printf("msga-recv ok\n");
	
	if(strncmp(msga->infor,"OK",2)==0)
	{
		puts("Register ok!");
		return;
	}
	else
	{
		puts("Register fail!");
		return;
		
	}
	
}
int do_login(int sockfd,MSGA *msga)
{
	msga->type=L;
	puts("input name>>>");
	scanf("%s",msga->name);
	puts("input password>>>");
	scanf("%s",msga->password);

	send(sockfd,msga,LEN_SMG,0);//用户名密码发给服务器
	printf("msga-send ok\n");
	recv(sockfd,msga,LEN_SMG,0);//等待服务器回应
	printf("msga-recv ok\n");
	if(strncmp(msga->infor,"OK",2)==0)
	{
		 puts("Login ok!");
		 if(strncmp(msga->idter,"ssuper",6)==0)
		 {
			return 1;
		 }
		 else
		 {
			return 2;
		 }
		 
	}
	else
	{
		puts("Login fail!");
		return -1;
	} 

}
void do_query(int sockfd,MSGA *msga)
{
	int n = 0;
	int i =0;
	char data[N];
	msga->type=Q;
	puts("请选择查找条件（1.id/2.name）：");
	scanf("%d",&i);
	if(i == 1)
	{
		puts("input id>>>");
		scanf("%d",&(msga->id));
	//	msga->name = NULL;
		puts("name 0");
		strcpy(msga->name,"chazhaoid");
		puts("name 1");
	}
	else if(i == 2)
	{
		puts("input name>>>");
		scanf("%s",msga->name);
	}
	else
	{	
		puts("input error,chose again");
		return;	
	}
	
	send(sockfd,msga,LEN_SMG,0);//id发给服务器
	recv(sockfd,msga,LEN_SMG,0); //等待服务器回应
	printf("msga-recv ok\n");
	
	if(strncmp(msga->infor,"OK",2)==0)
	{
		puts("Query ok!");
	//	recv(sockfd,data,LEN_SMG,0);//查询到的数据到client
		printf("id:%d name:%s age:%s addr:%s idter:%s\n",msga->id-48,msga->name,msga->age,msga->addr,msga->idter);
		return;
	}
	else
	{
		puts("Query fail!");
		return;
		
	}

	
	return;
}
void do_edit(int sockfd,MSGA *msga)
{
	int i;
	msga->type=B;
	puts("输入id");
	scanf("%d",&(msga->id));
	puts("输入要修改的信息（ 1.age / 2.addr / 3.password ）>>>");
	scanf("%d",&i);
	if(i == 1)
	{
		strcpy(msga->infor,"age");
		puts("输入age");
		scanf("%s",msga->age);
	}
	else if(i == 2)
	{
		strcpy(msga->infor,"addr");
		puts("输入addr");
		scanf("%s",msga->age);
	}
	else if(i == 3)
	{
		strcpy(msga->infor,"password");
		puts("输入password");
		scanf("%s",msga->age);
	}
	else
	{
		puts("输入错误，请重新选择");
		return;
	}	
	send(sockfd,msga,LEN_SMG,0);//id发给服务器
	recv(sockfd,msga,LEN_SMG,0);//等待服务器回应
	if(strncmp(msga->infor,"OK",2)==0)
	{
		puts("Edit ok!");
		return;
	}
	else
	{
		puts("Edit fail!");
		return;	
	}	
}
void do_add(int sockfd,MSGA *msga)
{
	msga->type=A;
//	puts("input id>>>");//id
//	scanf("%d",msga->id);
	strcpy(msga->idter,"num");
	puts("input name>>>");//addr
	scanf("%s",msga->name);
	puts("input age>>>");//
	scanf("%s",msga->age);
	puts("input password>>>");
	scanf("%s",msga->password);
	puts("input idter(ssuper/normal)>>>");
	scanf("%s",msga->idter);
	puts("input addr>>>");
	scanf("%s",msga->addr);
	scanf(NULL,msga->type);
	send(sockfd,msga,LEN_SMG,0);//数据发给务器
	recv(sockfd,msga,LEN_SMG,0);//等待服务器回应
	if(strncmp(msga->infor,"OK",2)==0)
	{
		puts("Add ok!");
		return;
	}
	else
	{
		puts("Add fail!");
		return;	
	}	
}
void do_delete(int sockfd,MSGA *msga)
{
	msga->type=D;
	puts("input id>>>");//id
	scanf("%d",&(msga->id));
	send(sockfd,msga,LEN_SMG,0);//数据发给务器
	recv(sockfd,msga,LEN_SMG,0);//等待服务器回应
	if(strncmp(msga->infor,"OK",2)==0)
	{
		puts("Delete ok!");
		return;
	}
	else
	{
		puts("Delete fail!");
		return;	
	}	
}






